package src.mian.java;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.io.IOException;

public class HDFSFileMove {
    public static void main(String[] args) {
        String hdfsUri = "hdfs://192.168.254.100:8020";
        String sourcePath = "/test/cat.txt";
        String destPath = "/animal/";
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS", hdfsUri);  // 设置默认的文件系统URI

        try {
            FileSystem fs = FileSystem.get(conf);
            Path source = new Path(sourcePath);
            Path dest = new Path(destPath);
            // Check if the source file exists
            if (fs.exists(source)) {
                // Move the file to the destination
                boolean success = fs.rename(source, dest);
                if (success) {
                    System.out.println("File moved successfully");
                } else {
                    System.out.println("Failed to move the file");
                }
            } else {
                System.out.println("Source file does not exist");
            }

            // Close the file system
            fs.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
